using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GottaGottaScrum.Models;

namespace GottaGottaScrum.Controllers
{   
    public class WorkItemsController : Controller
    {
		private readonly IWorkItemRepository workitemRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public WorkItemsController() : this(new WorkItemODataRepository())
        {
        }

        public WorkItemsController(IWorkItemRepository workitemRepository)
        {
			this.workitemRepository = workitemRepository;
        }

        //
        // GET: /WorkItems/

        public ViewResult Index()
        {
            return View(workitemRepository.All);
        }

        //
        // GET: /WorkItems/Details/5

        public ViewResult Details(int id)
        {
            return View(workitemRepository.Find(id));
        }

        //
        // GET: /WorkItems/Create

        public ActionResult Create()
        {
            return View();
        }

        public ViewResult Board()
        {
            return View(workitemRepository.All);
        }

        //
        // POST: /WorkItems/Create

        [HttpPost]
        public ActionResult Create(WorkItem workitem)
        {
            if (ModelState.IsValid) {
                workitemRepository.InsertOrUpdate(workitem);
                workitemRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }
        
        //
        // GET: /WorkItems/Edit/5
 
        public ActionResult Edit(int id)
        {
             return View(workitemRepository.Find(id));
        }

        //
        // POST: /WorkItems/Edit/5

        [HttpPost]
        public ActionResult Edit(WorkItem workitem)
        {
            if (ModelState.IsValid) {
                workitemRepository.InsertOrUpdate(workitem);
                workitemRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }

        //
        // GET: /WorkItems/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(workitemRepository.Find(id));
        }

        //
        // POST: /WorkItems/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            workitemRepository.Delete(id);
            workitemRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

